//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tool.ahibernate.util;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.tool.ahibernate.annotation.Column;
import com.tool.ahibernate.annotation.Id;
import com.tool.ahibernate.annotation.Table;
import java.lang.reflect.Field;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class TableHelper {
    private static final String TAG = "AHibernate";

    public TableHelper() {
    }

    public static <T> void createTablesByClasses(SQLiteDatabase db, Class<?>[] clazzs) {
        Class[] var5 = clazzs;
        int var4 = clazzs.length;

        for(int var3 = 0; var3 < var4; ++var3) {
            Class<?> clazz = var5[var3];
            createTable(db, clazz);
        }

    }

    public static <T> void dropTablesByClasses(SQLiteDatabase db, Class<?>[] clazzs) {
        Class[] var5 = clazzs;
        int var4 = clazzs.length;

        for(int var3 = 0; var3 < var4; ++var3) {
            Class<?> clazz = var5[var3];
            dropTable(db, clazz);
        }

    }

    public static <T> void createTable(SQLiteDatabase db, Class<T> clazz) {
        String tableName = "";
        if(clazz.isAnnotationPresent(Table.class)) {
            Table table = (Table)clazz.getAnnotation(Table.class);
            tableName = table.name();
        }

        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE ").append(tableName).append(" (");
        List<Field> allFields = joinFields(clazz.getDeclaredFields(), clazz.getSuperclass().getDeclaredFields());
        Iterator var6 = allFields.iterator();

        while(true) {
            Field field;
            do {
                if(!var6.hasNext()) {
                    sb.delete(sb.length() - 2, sb.length() - 1);
                    sb.append(")");
                    String sql = sb.toString();
                    Log.d("AHibernate", "crate table [" + tableName + "]: " + sql);
                    db.execSQL(sql);
                    return;
                }

                field = (Field)var6.next();
            } while(!field.isAnnotationPresent(Column.class));

            Column column = (Column)field.getAnnotation(Column.class);
            String columnType = "";
            if(column.type().equals("")) {
                columnType = getColumnType(field.getType());
            } else {
                columnType = column.type();
            }

            sb.append(column.name() + " " + columnType);
            if(column.length() != 0) {
                sb.append("(" + column.length() + ")");
            }

            if((!field.isAnnotationPresent(Id.class) || field.getType() != Integer.TYPE) && field.getType() != Integer.class) {
                if(field.isAnnotationPresent(Id.class)) {
                    sb.append(" primary key");
                }
            } else {
                sb.append(" primary key autoincrement");
            }

            sb.append(", ");
        }
    }

    public static <T> void dropTable(SQLiteDatabase db, Class<T> clazz) {
        String tableName = "";
        if(clazz.isAnnotationPresent(Table.class)) {
            Table table = (Table)clazz.getAnnotation(Table.class);
            tableName = table.name();
        }

        String sql = "DROP TABLE IF EXISTS " + tableName;
        Log.d("AHibernate", "dropTable[" + tableName + "]:" + sql);
        db.execSQL(sql);
    }

    private static String getColumnType(Class<?> fieldType) {
        return String.class == fieldType?"TEXT":(Integer.TYPE != fieldType && Integer.class != fieldType?(Long.TYPE != fieldType && Long.class != fieldType?(Float.TYPE != fieldType && Float.class != fieldType?(Short.TYPE != fieldType && Short.class != fieldType?(Double.TYPE != fieldType && Double.class != fieldType?(Blob.class == fieldType?"BLOB":"TEXT"):"DOUBLE"):"INT"):"FLOAT"):"BIGINT"):"INTEGER");
    }

    public static List<Field> joinFields(Field[] fields1, Field[] fields2) {
        Map<String, Field> map = new LinkedHashMap();
        Field[] var6 = fields1;
        int var5 = fields1.length;

        Field field;
        int var4;
        Column column;
        for(var4 = 0; var4 < var5; ++var4) {
            field = var6[var4];
            if(field.isAnnotationPresent(Column.class)) {
                column = (Column)field.getAnnotation(Column.class);
                map.put(column.name(), field);
            }
        }

        var6 = fields2;
        var5 = fields2.length;

        for(var4 = 0; var4 < var5; ++var4) {
            field = var6[var4];
            if(field.isAnnotationPresent(Column.class)) {
                column = (Column)field.getAnnotation(Column.class);
                if(!map.containsKey(column.name())) {
                    map.put(column.name(), field);
                }
            }
        }

        List<Field> list = new ArrayList();
        Iterator var10 = map.keySet().iterator();

        while(var10.hasNext()) {
            String key = (String)var10.next();
            Field tempField = (Field)map.get(key);
            if(tempField.isAnnotationPresent(Id.class)) {
                list.add(0, tempField);
            } else {
                list.add(tempField);
            }
        }

        return list;
    }
}
